//
//  MoneyKindSettingController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/8.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "MoneyKindSettingController.h"
#import "MoneyKindSettingCell.h"
@interface MoneyKindSettingController ()

@end

@implementation MoneyKindSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
}

- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MoneyKindSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoneyKindSettingCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MoneyKindSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MoneyKindSettingCell"];
    }
    if (indexPath.row==0) {
        cell.momeyKindLabel.text=@"人民币（CNY）";
    }else if (indexPath.row==1){
        
        cell.momeyKindLabel.text=@"美元（USD）";
    }else{
        cell.momeyKindLabel.text=@"澳元（AUD）";
    }
    cell.rightImage.hidden=YES;
    NSLog(@"price----->%@",[UserDefaults objectForKey:K_PRICE]);
    if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"1"]&&[cell.momeyKindLabel.text isEqualToString: @"人民币（CNY）"]) {
        cell.rightImage.hidden=NO;
    }else if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"2"]&&[cell.momeyKindLabel.text isEqualToString: @"美元（USD）"]){
        cell.rightImage.hidden=NO;
    }else if ([[UserDefaults objectForKey:K_PRICE] isEqualToString:@"3"]&&[cell.momeyKindLabel.text isEqualToString: @"澳元（AUD）"]){
        cell.rightImage.hidden=NO;
    }
    
    

    return cell;
    
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{
    MoneyKindSettingCell *cell = (MoneyKindSettingCell*)[tableView cellForRowAtIndexPath:indexPath];
    NSString*price_mod;
    if ([cell.momeyKindLabel.text isEqualToString: @"人民币（CNY）"]) {
        price_mod=@"1";
    }else if ([cell.momeyKindLabel.text isEqualToString: @"美元（USD）"]) {
        price_mod=@"2";
    }else{
        price_mod=@"3";
    }
    if ([UserDefaults objectForKey:K_LOGININFO_TOKEN]) {
        NSDictionary *params = @{@"price_mod":price_mod};
        [LSRequestManager putRequestWithURL:HUOBI(@"api/user/favSet")
                                     params:params
                                    success:^(NSDictionary *responseData) {
                                        if ([responseData[@"ret"] integerValue ] == 0) {
                                            //修改本地获取的语言文件-交替
                                            if ([cell.momeyKindLabel.text isEqualToString: @"人民币（CNY）"]) {
                                                [UserDefaults setObject:@"1" forKey:K_PRICE];
                                            }else if ([cell.momeyKindLabel.text isEqualToString: @"美元（USD）"]){
                                                [UserDefaults setObject:@"2" forKey:K_PRICE];
                                            }else if ([cell.momeyKindLabel.text isEqualToString: @"澳元（AUD）"]){
                                                [UserDefaults setObject:@"3" forKey:K_PRICE];
                                            }
                                            
                                            [UserDefaults synchronize];
                                            
                                            [self.tableView reloadData];
                                        } else {
                                            [MBProgressHUD showError:responseData[@"msg"]];
                                        }
                                    }
                                    failure:^(NSError *error) {
                                        
                                    }
                                    showHUD:NO];
    }else{
        if ([cell.momeyKindLabel.text isEqualToString: @"人民币（CNY）"]) {
            [UserDefaults setObject:@"1" forKey:K_PRICE];
        }else if ([cell.momeyKindLabel.text isEqualToString: @"美元（USD）"]){
            [UserDefaults setObject:@"2" forKey:K_PRICE];
        }else if ([cell.momeyKindLabel.text isEqualToString: @"澳元（AUD）"]){
            [UserDefaults setObject:@"3" forKey:K_PRICE];
        }
        [UserDefaults synchronize];
        
        [self.tableView reloadData];
    }
    
    
    

    
    
}


//-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
//
//{
//
//    MoneyKindSettingCell *cell = (MoneyKindSettingCell*)[tableView cellForRowAtIndexPath:indexPath];
//    cell.rightImage.hidden=NO;
//
//}
//- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath
//
//{
//
//    MoneyKindSettingCell *cell = (MoneyKindSettingCell*)[tableView cellForRowAtIndexPath:indexPath];
//    cell.rightImage.hidden=YES;
//
//}

@end
